﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.PlatformServices;
using System.Reactive.Threading;
using System.Reactive.Threading.Tasks;
using System.Text;
using System.Threading;

using agree;
using agree.configuration;
using agree.composition;
using agree.itsdb;

using alib.Debugging;
using alib.Dictionary;
using alib.Enumerable;
using alib.IO;
using alib.String;
using alib.Reflection;

namespace agree
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/// 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public static class LocalSchema
	{
		public static readonly agree.schema.Schema Instance;

		static LocalSchema()
		{
			var _tmp = agree.schema.SystemSchema.Instance;

#if DEBUG
			var ast = new assembly_stats(typeof(LocalSchema).Assembly, true);
#else
			var ast = new assembly_stats(typeof(LocalSchema).Assembly, false);
#endif

			_tmp.AddTypes(ast);

			_tmp.Realize();

			Instance = _tmp;
		}
	};


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/// 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public partial class MainClass : identity_node
	{
		const String startup_xaml = "app-default.xml";

		const String target_name = "batch_parse_to_chart";

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/// 
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		MainClass(String[] args)
			: base(SysObj.Instance)
		{
			var filename = SysObj.Instance.FindDefaultXamlFile(startup_xaml);

			var xlix = new f_xaml_load(this, LocalSchema.Instance).Func(filename);

			LoadNestedXaml(xlix);

			new f_map<IChartControl, IChartControl>(this, new f_console(this))
					.Bind((IFunctor<IUnit, IActiveObj<IChartControl>>)target)
					.Func(f_unit.Instance)
					.Subscribe();
			//new f_map<IChartControl, IChartControl>(this, new f_console(this))
			//		.Bind((IFunctor<IUnit, IActiveObj<IChartControl>>)target)
			//		.Func(f_unit.Instance)
			//		.Subscribe();
		}

		IIdentity target;

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/// 
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		void LoadNestedXaml(XamlLoadInstance xli, int nest = 0)
		{
			Workspace wksp = null;

			xli.Subscribe(_wksp =>
			{
				if (_wksp is StringCorpus)
				{
					Nop.X();
				}
				else if (_wksp is Workspace)
				{
					(wksp = (Workspace)_wksp).Subscribe(_item =>
					{
						Console.WriteLine(new String(' ', nest * 4) + "{0}:{1}", nest, _item.GetType()._Name());

						var xlin = _item as XamlLoadInstance;
						if (xlin != null)
							LoadNestedXaml(xlin, nest + 1);
					});
				}
			});

			xli.Wait();

			if (target == null && wksp != null)
				target = (IIdentity)wksp.NameScope.FindName(target_name);

#if false
			Lexicon lex;
			if ((lex = wksp.NameScope.FindName("thai_lex") as Lexicon) != null)
			{
				var qqq = lex.lex_lookup
					.Select(kvp => kvp.Value)
					.Where(arr => !arr.IsDistinct(le => le.InstanceType))
					.ToArray();

				var tu = lex.tu;
				
				var q2 = lex.LexicalEntries
					.Where(le => le.RelKeys.Any(rk => tu.TypeNameOrStringValue(rk.pred).Any(ch => alib.Character.Thai._character_thai_ext.IsThai(ch))))
					.ToArray();

				var q4 = lex.lex_lookup.Keys.ToArray();

				System.IO.File.WriteAllLines(@"D:\Programming\thai-language\tl-exp\tdl-lexicon-words.txt", q4, Encoding.UTF8);

				Nop.X();
			}
#endif
			//Console.WriteLine("completed '{0}'", xli.Filename);
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/// 
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		static void Main(String[] args)
		{
			new MainClass(args);

			SysObj.Idle();

			Console.WriteLine();
			if (Debugger.IsAttached)
			{
				Console.WriteLine("done! press <enter> to exit");
				Console.ReadKey();
			}
		}
	};
}
